package com.qidian.mes.md.service.impl;

import com.qidian.mes.md.mapper.MdItemTypeMapper;
import com.qidian.mes.md.service.MdItemService;
import com.qidian.mes.md.service.MdItemTypeService;
import com.qidian.mes.pojo.MdItemType;
import com.qidian.mes.util.pojo.MESResult;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @Author 32郗建狄
 * @Company GX631
 * Time 2023/11/2 10:16
 * Description
 */
@Service
@Transactional
public class MdItemTypeServiceImpl implements MdItemTypeService {
    @Autowired
    MdItemTypeMapper mdItemTypeMapper;
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public List<MdItemType> selectByParentId(Long id) {
        MdItemTypeService mdItemTypeService = (MdItemTypeService) AopContext.currentProxy();
        List<MdItemType> mdItemTypes = mdItemTypeMapper.selectByParentId(id);
        for (MdItemType mdItemType:mdItemTypes){
            if (mdItemType.getParent().equals(1)){  //  如果是父级类目就递归查询其所有子类目
                mdItemType.setChildren(mdItemTypeService.selectByParentId(mdItemType.getItemTypeId()));
            }
        }
        return mdItemTypes;
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public MESResult getAll() {
        MdItemTypeService mdItemTypeService = (MdItemTypeService) AopContext.currentProxy();
        // 先查询一级类目
        // 后续子类目根据parent标识 递归查询
        List<MdItemType> mdItemTypes = mdItemTypeService.selectByParentId(0L);
        return MESResult.ok(mdItemTypes);
    }

    @Override
    public MESResult get(Long id) {
        if (Objects.nonNull(id)){
            return MESResult.ok(mdItemTypeMapper.get(id));
        }
        return null;
    }

    @Override
    public MESResult update(MdItemType mdItemType) {
        if (Objects.nonNull(mdItemType)){
            return MESResult.ok(mdItemTypeMapper.update(mdItemType));
        }
        return null;
    }

    @Override
    public MESResult save(MdItemType mdItemType) {
        if (Objects.nonNull(mdItemType)){
            if (mdItemType.getParentTypeId() != 0) {    //  判断是不是在最顶层类目下
                MdItemType parentType = mdItemTypeMapper.get(mdItemType.getParentTypeId());
                if (parentType.getParent() == 2) {   //添加一个新的分类时修改其父节点标识
                    parentType.setParent(1);
                    mdItemTypeMapper.update(parentType);
                    return MESResult.ok(mdItemTypeMapper.save(mdItemType));
                }
                return MESResult.ok(mdItemTypeMapper.save(mdItemType));//如果父节点标识正确那么不用修改 直接插入新的就行
            }

            return MESResult.ok(mdItemTypeMapper.save(mdItemType));
        }
        return null;
    }

    @Override

    public MESResult delete(Long id) {

        MdItemType mdItemType = null;
        if (Objects.nonNull(id)){
            mdItemType = mdItemTypeMapper.get(id);  //先找到要删除的分类
            if (mdItemType.getParent() == 2){   //如果是叶子  直接删
                mdItemTypeMapper.delete(id);
                //  如果该分类的父类目没有其他子分类    更改该分类的父类目的父节点标识  改为"叶子"
                if (mdItemTypeMapper.countByParentTypeId(mdItemType.getParentTypeId()) == 0) {
                    MdItemType typeParent = mdItemTypeMapper.get(mdItemType.getParentTypeId());
                    typeParent.setParent(2);
                    mdItemTypeMapper.update(typeParent);
                    return MESResult.ok();
                }
                return MESResult.ok();
            }
        }
        return null;
    }

    @Override
    public MESResult searchByNameAndEnable(MdItemType mdItemType) {
        if (Objects.nonNull(mdItemType)){
            if (mdItemType.getItemTypeName().equals("undefined")){
                mdItemType.setItemTypeName(null);
            }
            return MESResult.ok(mdItemTypeMapper.searchByNameAndEnable(mdItemType));
        }
        return null;
    }
}
